﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChilishopDataLibrary
{
    public sealed class ChilishopData
    {
        private static List<Product> allProducts = Product.GetAllProducts();
        private static List<Category> allCategories = Category.GetAllCategories();
        public static List<Category> GetCategories()
        {
            return allCategories;
        }

        public static List<Product> GetProductsWithCategory(string categoryId)
        {
            if (string.IsNullOrEmpty(categoryId))
                return allProducts;
            else
                return allProducts.Where(p => p.CategoryId.ToString().Equals(categoryId)).ToList();
        }

        public static Product GetProductWithId(string prodId)
        {
            List<Product> prod=allProducts.Where(p => p.Id.ToString().Equals(prodId)).ToList();
            if (prod.Count <= 1)
                return prod.FirstOrDefault();
            else
                throw new Exception("Incoherent data for products! More Products with same Id!");                         
        }

        public static List<Product> GetProductWithNew()
        {
            return allProducts.Where(p => p.IsNew).ToList();            
        }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Category(int p1, string p2)
        {
            // TODO: Complete member initialization
            this.Id = p1;
            this.Name = p2;
        }

        internal static List<Category> GetAllCategories()
        {
            List<Category> rez = new List<Category>();
            string filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + @"App_GlobalResources\MockData\categories.csv";
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadLine();
                //string s = Console.ReadLine();
                while (str != null)
                {
                    string[] xu = new String[2];
                    xu = str.Split(',');
                    rez.Add(new Category(int.Parse(xu[0]), xu[1]));
                    str = sr.ReadLine();
                } sr.Close();
            }
            return rez;
        }
    }

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public int CategoryId { get; set; }
        public string ImagePath { get; set; }
        public bool IsNew { get; set; }
        public string DetailPageLink
        {
            get
            {
                return @"/pages/ProductDetailPage.aspx?productId=" + Id;
            }
        }

        public Product(int id, string name, string description, decimal price, int catId,
     string imagePath, bool isNew = false)
        {
            this.Id = id;
            this.Name = name;
            this.Description = description;
            this.Price = price;
            this.CategoryId = catId;
            this.ImagePath = imagePath;
            this.IsNew = isNew;
        }
        internal static List<Product> GetAllProducts()
        {
            List<Product> rez = new List<Product>();
            string filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + @"App_GlobalResources\MockData\products.csv";
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));
                string str = sr.ReadLine();
                //string s = Console.ReadLine();
                while (str != null)
                {
                    string[] xu = new String[7];
                    xu = str.Split(',');
                    rez.Add(new Product(int.Parse(xu[0]), xu[1], xu[2], decimal.Parse(xu[3]), int.Parse(xu[4]), xu[5],
                                        xu[6].Equals("1")?true:false));
                    str = sr.ReadLine();
                } sr.Close();
            }
            return rez;           
        }
    }
}